package dp;

import java.util.Arrays;

/**
 * 435. 无重叠区间
 */
public class P435 {
    public int eraseOverlapIntervals(int[][] intervals) {
        Arrays.sort(intervals, (a, b) -> {
            if (a[1] - b[1] == 0) {
                return a[0] - b[0];
            } else {
                return a[1] - b[1];
            }
        });
        int begin = intervals[0][0];
        int total = 0;
        for (int i = 0; i < intervals.length; i++) {
            int[] range = intervals[i];
            if (begin <= range[0]) {
                begin = range[1];
                total++;
            }
        }
        return intervals.length - total;
    }

    public static void main(String[] args) {
        int[][] intervals = new int[][]{{-52, 31}, {-73, -26}, {82, 97}, {-65, -11}, {-62, -49}, {95, 99}, {58, 95}, {-31, 49}, {66, 98}, {-63, 2}, {30, 47}, {-40, -26}};

        int count = new P435().eraseOverlapIntervals(intervals);
        System.out.println(count);
    }
}
